"use strict";
const Controller = require("egg").Controller;
const code = require("../errorCode.js");

class CRUD extends Controller {
  async findAll(){
    console.log('findAll')
    const { ctx, service } = this;
    const db = ctx.params.db;
    const name = ctx.params.name;
    let where = ctx.request.body.where;
    if (typeof where == 'string'){
      where = JSON.parse(where)
    }
    if (ctx.request.header.operation.SELECT) {
      let colList = ctx.getColumnsArr(
        ctx.request.body.columns,
        ctx.request.header.operation.columns,
        ctx.request.header.user.role
      );
      if (
        colList.length > 0 ||
        ctx.request.header.user.role.indexOf("ADMIN") >= 0
      ) {
        ctx.body = await service.mysql.findAll(
          db,
          name,
          where,
          colList.join(","),
        );
      } else {
        ctx.body = ctx.app.error(
          code.AUTH_COLS,
          `${db}.${name}有查询权限，但无权限显示字段！`
        );
      }
    } else {
      ctx.body = ctx.app.error(code.AUTH, `${db}.${name}查询权限不足！`);
    }
  }
  async find() {
    const { ctx, service } = this;
    const db = ctx.params.db;
    const name = ctx.params.name;
    const where = ctx.request.body.where;
    const orders = ctx.request.body.orders;
    const offset = parseInt(ctx.request.body.offset) || 0;
    const limit =
      parseInt(ctx.request.body.limit) || this.app.config.mysql.defaultLimit;
    console.log('SELECT:',ctx.request.header.operation.SELECT)
    if (ctx.request.header.operation.SELECT) {
      let colList = ctx.getColumnsArr(
        ctx.request.body.columns,
        ctx.request.header.operation.columns,
        ctx.request.header.user.role
      );
      if (
        colList.length > 0 ||
        ctx.request.header.user.role.indexOf("ADMIN") >= 0
      ) {
        ctx.body = await service.mysql.find(
          db,
          name,
          where,
          colList.join(","),
          orders,
          offset,
          limit
        );
      } else {
        ctx.body = ctx.app.error(
          code.AUTH_COLS,
          `${db}.${name}有查询权限，但无权限显示字段！`
        );
      }
    } else {
      ctx.body = ctx.app.error(code.AUTH, `${db}.${name}查询权限不足！`);
    }
  }

  async findOne() {
    const { ctx, service } = this;
    const db = ctx.params.db;
    const name = ctx.params.name;
    const where = ctx.request.body.where;
    if (ctx.request.header.operation.SELECT) {
      let colList = ctx.getColumnsArr(
        ctx.request.body.columns,
        ctx.request.header.operation.columns,
        ctx.request.header.user.role
      );
      if (
        colList.length > 0 ||
        ctx.request.header.user.role.indexOf("ADMIN") >= 0
      ) {
        ctx.body = await service.mysql.findOne(
          db,
          name,
          where,
          colList.join(","),
        );
      } else {
        ctx.body = ctx.app.error(
          code.AUTH_COLS,
          `${db}.${name}有查询权限，但无权限显示字段！`
        );
      }
    } else {
      ctx.body = ctx.app.error(code.AUTH, `${db}.${name}查询权限不足！`);
    }
  }

  async findById() {
    const { ctx, service } = this;
    const db = ctx.params.db;
    const name = ctx.params.name;
    const where = ctx.request.body.where;
    const orders = ctx.request.body.orders;
    const offset = parseInt(ctx.request.body.offset) || 0;
    const limit =
      parseInt(ctx.request.body.limit) || this.app.config.mysql.defaultLimit;
    if (ctx.request.header.operation.SELECT) {
      let colList = ctx.getColumnsArr(
        ctx.request.body.columns,
        ctx.request.header.operation.columns,
        ctx.request.header.user.role
      );
      if (
        colList.length > 0 ||
        ctx.request.header.user.role.indexOf("ADMIN") >= 0
      ) {
        ctx.body = await service.mysql.findById(db, name, id, colList);
      } else {
        ctx.body = ctx.app.error(
          code.AUTH_COLS,
          `${db}.${name}有查询权限，但无权限显示字段！`
        );
      }
    } else {
      ctx.body = ctx.app.error(code.AUTH, `${db}.${name}查询权限不足！`);
    }
  }

  async add() {
    const { ctx, service } = this;
    const db = ctx.params.db;
    const name = ctx.params.name;
    const user = ctx.request.header.user || {
      id: null,
      organ: null,
      role: null,
    };
    let data = { ...ctx.request.body };
    if (ctx.request.header.operation.INSERT) {
      let colList = ctx.getColumnsArr(
        ctx.request.body.columns,
        ctx.request.header.operation.columns,
        user.role
      );
      if (colList.join(",") != "*") {
        for (let key in data) {
          if (colList.indexOf(key) < 0) {
            delete data[key];
          }
        }
      }
      ctx.body = await service.mysql.add(db, name, data, user);
    } else {
      ctx.body = ctx.app.error(code.AUTH, `${db}.${name}插入权限不足！`);
    }
  }

  async edit() {
    const { ctx, service } = this;
    const db = ctx.params.db;
    const name = ctx.params.name;
    const user = ctx.request.header.user || {
      id: null,
      organ: null,
      role: null,
    };
    let data = { ...ctx.request.body };
    if (ctx.request.header.operation.UPDATE) {
      let colList = ctx.getColumnsArr(
        ctx.request.body.columns,
        ctx.request.header.operation.columns,
        user.role
      );
      if (colList.join(",") != "*") {
        for (let key in data) {
          if (colList.indexOf(key) < 0) {
            delete data[key];
          }
        }
      }
      ctx.body = await service.mysql.edit(db, name, data, user);
    } else {
      ctx.body = ctx.app.error(code.AUTH, `${db}.${name}更新权限不足！`);
    }
  }

  async remove() {
    const { ctx, service } = this;
    const db = ctx.params.db;
    const name = ctx.params.name;
    const id = ctx.params.id;
    if (ctx.request.header.operation.DELETE) {
      ctx.body = await service.mysql.remove(db, name, id);
    } else {
      ctx.body = ctx.app.error(code.AUTH, `${db}.${name}删除权限不足！`);
    }
  }

  /**
   * 执行mapper/*.json中的sql语句
   */
  async excute() {
    const { ctx, service } = this;
    const db = ctx.params.db;
    const name = ctx.params.name;
    if (ctx.request.header.operation.EXCUTE) {
      ctx.body = await service.mysql.excute(db, name, ctx.request.body);
    } else {
      ctx.body = ctx.app.error(code.AUTH, `${db}.${name}执行权限不足！`);
    }
  }

  /**
   * 根据条件参数返回树
   */
  async tree() {
    const { ctx, service } = this;
    const db = ctx.params.db;
    const name = ctx.params.name;
    const field = ctx.params.field;
    if (ctx.request.header.operation.TREE) {
      ctx.body = await service.mysql.tree(db, name, field, ctx.request.body);
    } else {
      ctx.body = ctx.app.error(code.AUTH, `${db}.${name}获取树权限不足！`);
    }
  }
}

module.exports = CRUD;
